﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using COM.Data;
using COM.Service;
using COM.Web;
using SampleApp.Domain;
using SampleApp.Service;
using SampleApp.Web.Service;
using SampleApp.Web.SysModule.Models;


namespace SampleApp.Web.SysModule.Controllers
{
    public class UserInfoController : Controller
    {
        private ISysUserService sysUserService = Unity.Instance.GetService<ISysUserService>();
        private ISysDeptService sysDeptService = Unity.Instance.GetService<ISysDeptService>();
        private IRuntime runtime = Unity.Instance.GetService<IRuntime>();

        public ActionResult Index()
        {
            return Redirect(string.Format("/SysModule/SysUser/ReadOnly/{0}", this.runtime.Permission.CurrentUser.UserCode));
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if(this.sysUserService.CheckUserPassword(this.runtime.Permission.CurrentUser.UserCode,model.OldPassword))
                {
                    this.sysUserService.SetUserPassword(this.runtime.Permission.CurrentUser.UserCode, model.NewPassword);
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "原密码错误.");
                }
            }

            return View(model);
        }

        [Authorize]
        public ActionResult ChangePassword()
        {
            return View();
        }


        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }


    }
}